import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

// import Layout from '@/layout/Layout.vue'
import { Layout } from '@/utils/routerHelper'
import ERROR from '@/views/admin/Error/ERROR.vue'

// 静态路由
export const staticRoutes = [
  {
    path: '/',
    component: Layout,
    // redirect: "/dashboard/analysis",
    name: "layout",
    meta: {
      title: "home",
      icon: "vi-ant-design:dashboard-filled",
      hidden: true
    },
  },
  {
    path: "/error",
    component: ERROR,
    name: "Error",
    meta: {
      hidden: true
    }
  },
  {
    path: "/login",
    component: () => import("@/views/admin/login/Login.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: '/404',
    component: () => import("@/views/admin/Error/NotFound.vue"),
    meta: {
      hidden: true
    }
  }
]

// 动态路由
export const AsyncRouter = [
  {
    path: "/welcome",
    name: "Welcome",
    meta: {
      title: "Welcome",
      hidden: true,
    },
    component: Layout,
    children: [
      {
        path: "index",
        name: "Welcome",
        meta: {
          title: "Welcome",
        },
        component: () => import("@/views/admin/welcome/Welcome.vue"),
      },
    ],
  },
  {
    path: "/dashboard",
    name: "Dashboard",
    component: Layout,
    redirect: "/dashboard/analysis",
    meta: {
      title: "首页",
      icon: "el:House"
    },
    children: [
      {
        path: "analysis",
        name: "Analysis",
        component: () => import("@/views/admin/dashboard/Analysis.vue"),
        meta: {
          title: "分析页",
        },
      },
      {
        path: "workplace",
        name: "Workplace",
        component: () => import("@/views/admin/dashboard/Workplace.vue"),
        meta: {
          title: "工作台",
        },
      },
    ]
  },
  {
    path: "/authorization",
    component: Layout,
    name: "Authorization",
    meta: {
      title: "权限管理",
      icon: "el:SetUp"
    },
    children: [
      {
        path: "department",
        name: "Department",
        component: () => import("@/views/admin/authorization/Department.vue"),
        meta: {
          title: "部门管理",
        },
      },
      {
        path: "user",
        name: "User",
        component: () => import("@/views/admin/authorization/user/User.vue"),
        meta: {
          title: "用户管理",
        },
      },
      {
        path: "menu",
        name: "Menu",
        component: () => import("@/views/admin/authorization/menu/Menu.vue"),
        meta: {
          title: "菜单管理",
        },
      },
      {
        path: "role",
        name: "Role",
        component: () => import("@/views/admin/authorization/role/Role.vue"),
        meta: {
          title: "角色管理",
        },
      }
    ]
  },
  {
    path: "/system",
    name: "System",
    component: Layout,
    redirect: "/system/system",
    meta: {
      title: "系统管理",
      icon: "el:Coin"
    },
    children: [
      {
        path: "log",
        name: "Log",
        component: () => import("@/views/admin/dashboard/Analysis.vue"),
        meta: {
          title: "日志管理",
        },
      },
      {
        path: "public",
        name: "Public ",
        component: () => import("@/views/admin/dashboard/Analysis.vue"),
        meta: {
          title: "公告管理",
        },
      }
    ]
  }
]

const router = createRouter({
  routes: [...staticRoutes] as RouteRecordRaw[],
  strict: true,
  history: createWebHashHistory(),
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (Routes: any) => {

  const newStaticRoutes = Routes ? Routes : staticRoutes

  // 创建一个新的路由实例
  const newRouter = createRouter({
    routes: [...newStaticRoutes] as RouteRecordRaw[],
    strict: true,
    history: createWebHashHistory(),
    scrollBehavior: () => ({ left: 0, top: 0 })
  });

  // 重置路由
  router.getRoutes().forEach(route => {
    router.removeRoute(route.name!);
  });

  // 重新添加新的路由配置
  newRouter.getRoutes().forEach(route => {
    router.addRoute(route);
  });
};




export const setupRouter = (app: any) => {
  app.use(router)
}


export default router